#!/usr/bin/perl -w

# Copyright: 2017 Dmitry Shachnev <mitya57@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=head1 NAME

dh_qmlcdeps - generate proper dependencies for QML cache files

=head1 SYNOPSIS

B<dh_qmlcdeps> [S<I<debhelper options>>]

=head1 DESCRIPTION

B<dh_qmlcdeps> is a debhelper program that looks for F<*.qmlc> files
(generated by B<qmlcachegen>) in the package temporary locations, performs
sanity check on them, and adds a dependency on B<libqt5qml5> package to
B<${qmlc:Depends}> substitution variable to make sure its version matches
the Qt version in these files.

You can pass B<--with qmlcdeps> to L<dh(1)> to make it automatically call
B<dh_qmlcdeps> after B<dh_install>.

=cut

use strict;
use warnings;
use File::Find;
use Debian::Debhelper::Dh_Lib;

init(options => {});

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $tmpdir = tmpdir($package);
    next unless -d $tmpdir;
    my $qt_version_bin;

    find({
        wanted => sub {
            my $filename = $_;
            return unless ($filename =~ m/\.qmlc$/);
            open (my $fh, "<", $filename);
            binmode $fh;
            read ($fh, my $magic, 8);
            if ($magic ne "qv4cdata") {
                close $fh;
                error("Unknown .qmlc file: $File::Find::name");
            }
            read ($fh, my $struct_version, 4);
            read ($fh, my $file_qt_version_bin, 4);
            close $fh;
            if ($qt_version_bin and $file_qt_version_bin ne $qt_version_bin) {
                error("Package $package contains .qmlc files built for different Qt versions");
            }
            $qt_version_bin = $file_qt_version_bin;
        }
    }, $tmpdir);

    $qt_version_bin or next;
    my @qt_version = unpack("C4", $qt_version_bin);
    my $qt_version_str = "$qt_version[2].$qt_version[1].$qt_version[0]";
    $qt_version[2] == 5 or error("Qt version $qt_version_str is not supported");
    my $qt_next_version_str = "$qt_version[2].$qt_version[1]." . ($qt_version[0] + 1);

    addsubstvar($package, "qmlc:Depends", "libqt5qml5 (>= $qt_version_str~)");
    addsubstvar($package, "qmlc:Depends", "libqt5qml5 (<< $qt_next_version_str~)");
}

=head1 SEE ALSO

L<debhelper(7)>

=head1 AUTHOR

Dmitry Shachnev <mitya57@debian.org>

=cut
